package com.getpebble.android.framework.notification;

import android.content.Context;
import android.os.Bundle;
import android.os.SystemClock;
import com.getpebble.android.PebbleApplication;
import com.getpebble.android.R;
import com.getpebble.android.common.core.async.PblAsyncTask;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.model.FrameworkState;
import com.getpebble.android.common.model.PblDevice;
import com.getpebble.android.framework.comm.MessageRouter;
import com.getpebble.android.framework.endpoint.EndpointRequest;
import com.getpebble.android.framework.endpoint.EndpointRequest.EndpointAction;
import com.getpebble.android.framework.endpoint.EndpointRequest.EndpointArgumentKeys;
import com.getpebble.android.framework.protocol.EndpointId;
import com.getpebble.android.framework.util.ByteUtils;
import com.getpebble.android.notifications.model.PblNotification;
import com.getpebble.android.notifications.model.PblNotification.LegacyNotification;
import com.getpebble.android.notifications.model.PblNotification.Source;
import com.sun.mail.imap.protocol.ListInfo;
import com.sun.mail.smtp.SMTPMessage;

public class NotificationSender {
    public static final String TAG = NotificationSender.class.getSimpleName();
    private Context mContext;
    private FrameworkState mFrameworkState;
    private boolean mPhoneDemoInProgress = false;

    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$framework$notification$NotificationSender$NotificationDemoType = new int[NotificationDemoType.values().length];

        static {
            try {
                $SwitchMap$com$getpebble$android$framework$notification$NotificationSender$NotificationDemoType[NotificationDemoType.EMAIL.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$notification$NotificationSender$NotificationDemoType[NotificationDemoType.SMS.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$notification$NotificationSender$NotificationDemoType[NotificationDemoType.PHONE_CALL.ordinal()] = 3;
            } catch (NoSuchFieldError e3) {
            }
        }
    }

    public enum NotificationDemoType {
        EMAIL,
        SMS,
        PHONE_CALL
    }

    public class PhoneDemoTask extends PblAsyncTask {
        public boolean doInBackground() {
            NotificationSender.this.setPhoneDemoInProgress(true);
            String number = NotificationSender.this.getContext().getString(R.string.notification_demo_phone_sender);
            String name = NotificationSender.this.getContext().getString(R.string.notification_demo_phone_name);
            byte[] cookie = ByteUtils.randomCookie();
            Bundle args = new Bundle();
            args.putString(EndpointArgumentKeys.PHONE_NUMBER.toString(), number);
            args.putString(EndpointArgumentKeys.PHONE_NAME.toString(), name);
            args.putByteArray(EndpointArgumentKeys.PHONE_COOKIE.toString(), cookie);
            NotificationSender.this.sendRequestToConnectedDevice(new EndpointRequest(EndpointId.PHONE_CONTROL, EndpointAction.SEND_PHONE_INCOMING_CALL_NOTIFICATION, args));
            EndpointRequest request = new EndpointRequest(EndpointId.PHONE_CONTROL, EndpointAction.SEND_PHONE_RING_NOTIFICATION, args);
            NotificationSender.this.sendRequestToConnectedDevice(request);
            for (int i = 0; i < 3; i++) {
                SystemClock.sleep(2000);
                NotificationSender.this.sendRequestToConnectedDevice(request);
            }
            SystemClock.sleep(1000);
            NotificationSender.this.sendRequestToConnectedDevice(new EndpointRequest(EndpointId.PHONE_CONTROL, EndpointAction.SEND_PHONE_START_NOTIFICATION, args));
            SystemClock.sleep(1000);
            NotificationSender.this.sendRequestToConnectedDevice(new EndpointRequest(EndpointId.PHONE_CONTROL, EndpointAction.SEND_PHONE_END_NOTIFICATION, args));
            NotificationSender.this.setPhoneDemoInProgress(false);
            return true;
        }

        public void onTaskSuccess() {
            NotificationSender.this.mFrameworkState.setNotificationDemoResult(true);
        }

        public void onTaskFailed() {
            NotificationSender.this.mFrameworkState.setNotificationDemoResult(false);
        }
    }

    public NotificationSender(Context context, FrameworkState frameworkState) throws IllegalArgumentException {
        if (context == null) {
            throw new IllegalArgumentException("'Context' cannot be null!");
        } else if (frameworkState == null) {
            throw new IllegalArgumentException("'frameworkState' cannot be null!");
        } else {
            this.mContext = context;
            this.mFrameworkState = frameworkState;
        }
    }

    private boolean sendRequestToConnectedDevice(EndpointRequest request) {
        PblDevice connectedDevice = PebbleApplication.getConnectedDevice();
        if (connectedDevice == null) {
            Trace.error(TAG, "Could not send request, connected device was null!");
            return false;
        }
        Trace.debug(TAG, "Sending test notification to " + connectedDevice.getName());
        return MessageRouter.getRouter(connectedDevice).onRequest(request, null);
    }

    public void sendNotificationDemoMessage(NotificationDemoType type) {
        switch (AnonymousClass1.$SwitchMap$com$getpebble$android$framework$notification$NotificationSender$NotificationDemoType[type.ordinal()]) {
            case SMTPMessage.RETURN_FULL /*1*/:
                sendEmailDemoMessage();
                return;
            case SMTPMessage.RETURN_HDRS /*2*/:
                sendSmsDemoMessage();
                return;
            case ListInfo.INDETERMINATE /*3*/:
                sendPhoneCallDemoMessage();
                return;
            default:
                return;
        }
    }

    private Context getContext() {
        return this.mContext;
    }

    private void sendEmailDemoMessage() {
        String sender = getContext().getString(R.string.notification_demo_email_sender);
        String body = getContext().getString(R.string.notification_demo_email_body);
        String subject = getContext().getString(R.string.notification_demo_email_subject);
        LegacyNotification content = new LegacyNotification();
        content.title = sender;
        content.body = subject + "\n" + body;
        PblNotificationProcessor.processNotification(PblNotification.from(content, Source.DEMO));
    }

    private void sendSmsDemoMessage() {
        String sender = getContext().getString(R.string.notification_demo_phone_sender);
        String body = getContext().getString(R.string.notification_demo_sms_body);
        LegacyNotification content = new LegacyNotification();
        content.title = sender;
        content.body = body;
        PblNotificationProcessor.processNotification(PblNotification.from(content, Source.DEMO));
    }

    private synchronized void sendPhoneCallDemoMessage() {
        if (!this.mPhoneDemoInProgress) {
            new PhoneDemoTask().submit();
        }
    }

    private synchronized void setPhoneDemoInProgress(boolean inProgress) {
        this.mPhoneDemoInProgress = inProgress;
    }
}
